<?php

/**
 * Maikuro Framework
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2010, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

/**
 * @see Validators
 */
require_once 'Validators.php';

/**
 * The Deployer class contains a single static method that merges class files
 * into a single file to improve performance.
 */
class Deployer
{
    /**
     * Deploys the application by merging class files into a single file. This
     * can dramatically improve performance (depending on how many files that
     * are included normally).
     *
     * @param string|array $paths
     * @param string $target
     * @param boolean $log
     * @return void
     * @todo Logging
     * @todo Support for stripping whitespace
     */
    public static function deploy($paths, $target = 'deploy.php', $log = false)
    {
        if (!is_array($paths)) {
            $paths = array($paths);
        }

        foreach ($paths as $path) {

            $iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($path),
                RecursiveIteratorIterator::LEAVES_ONLY
            );

            foreach ($iterator as $file) {
                if ($file->isFile()
                    && Validators::endsWith($file->getFileName(), '.php')) {
                    if ($log) {
                        // TODO: $logger->info("Including $file->getPathName()");
                    }
                    require_once $file->getPathName();
                }
            }
        }

        $deployFileContent = '';

        $interfacesAndClasses = array_merge(get_declared_interfaces(), get_declared_classes());
        foreach ($interfacesAndClasses as $interfaceOrClass) {
            $reflection = new ReflectionClass($interfaceOrClass);
            $fileName = $reflection->getFileName();
            if (!empty($fileName)) {
                $fileArray = file($fileName);
                for ($i = ($reflection->getStartLine() - 1); $i < $reflection->getEndLine(); $i++) {
                    $deployFileContent .= $fileArray[$i];
                }
                $deployFileContent .= "\n";
            }
        }
        $deployFileContent = "<?php\n" . $deployFileContent;

        $fileWritten = file_put_contents($target, $deployFileContent);
        //file_put_contents($target, php_strip_whitespace($target));

        if ($fileWritten === false) {
            throw new Exception('Could not write the deploy file.');
        }
    }
}